package com.application.Activity;

import java.util.ArrayList;

import ReadWriteXML.CMember;
import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.Network.Protocol;
import com.application.PhanQuyen.Quyen;


public class frmDanhSachThanhVienActivity extends Activity {
	int port = Protocol.PORT;
	ListView lvDanhSachThanhVien;

	ArrayList<CMember> memberList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_danh_sach_thanh_vien);
		lvDanhSachThanhVien = (ListView) findViewById(R.id.lvDanhSachThanhVien);
		fillListView();
		registerForContextMenu(lvDanhSachThanhVien);
	}

	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
	private void fillListView() {
		 memberList = ReadXML.ReadMember(ReadWriteFileController.MEMBER_PATH);
		ArrayAdapter<CMember> adapter = new ArrayAdapter<CMember>(this, android.R.layout.simple_list_item_1, memberList);
		lvDanhSachThanhVien.setAdapter(adapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.lvDanhSachThanhVien) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle("Thao tác");
			menu.addSubMenu(Menu.NONE, 1, 1, "Mời ra khỏi nhóm");
			menu.addSubMenu(Menu.NONE, 2, 2, "Chat riêng");
			menu.addSubMenu(Menu.NONE, 3, 3, "Yêu cầu ngừng thảo luận");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		if (menuItemIndex == 1) {
			Log.d("Phu","vao moi nguoi khac ra ngoai");
			Quyen myRule = NetworkController._peer.getAccount().getRuleById(5);
			if (myRule != null && myRule.getSend()) {
				Log.d("Phu","co quyen moi nguoi khac ra ngoai");
				Toast.makeText(getBaseContext(), "mời ra khỏi nhóm", 1).show();
				Intent data = new Intent();
				data.putExtra(FrmTabActivity.IP_RESULT, memberList.get(info.position).get_ip());
				setResult(FrmTabActivity.CODE_KICK, data);
			//	startActivity(data);
				
				finish();
			} else {
				finish();
				Show(Protocol.NON_RULE_MESSAGE);
			}
		} else if (menuItemIndex == 2) {
			Log.d("Phu","vao chat rieng");
			Quyen myRule = NetworkController._peer.getAccount().getRuleById(7);
			if (myRule != null && myRule.getSend()) {
				Intent data=new Intent(this,frmChatPeerActivity.class);
				data.putExtra(frmChatPeerActivity.IP_CHAT_PEER, memberList.get(info.position).get_ip());
				data.putExtra(frmChatPeerActivity.USERNAME, memberList.get(info.position).get_userName());
				setResult(frmChatPeerActivity.CODE_CHATPEER, data);
				
				
				startActivity(data);
				finish();
			}
			else{
				finish();
				NetworkController.ShowMessageNoticeTAB(Protocol.NON_RULE_MESSAGE);
			}
		} else if (menuItemIndex == 3) {
			Log.d("Phu","vao yeu cau ngung thao luan");
			Quyen quyen=NetworkController._peer.getAccount().getRuleById(6);
			if (quyen != null && quyen.getSend()) {
				Log.d("Phu","co quyen yeu cau ngung thao luan");
				Toast.makeText(getBaseContext(), "Yêu cầu ngừng thảo luận", 1).show();
				Intent data = new Intent();
				data.putExtra(FrmTabActivity.IP_RESULT, memberList.get(info.position).get_ip());
				setResult(FrmTabActivity.CODE_STOP_TALK, data);
			//	startActivity(data);
				//startActivityForResult(data, FrmTabActivity.CODE_STOP_TALK);
				finish();
			} 
			else {
				finish();
				Show(Protocol.NON_RULE_MESSAGE);
			}
			
		}
		return true;
	}
	public void Show(String message) {
		// Toast.makeText(this, "hehe Xin chao", Toast.LENGTH_SHORT).show();
		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Thông báo").setMessage(message)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).create();
		dlg.show();
	}
}
